package com.chagee.store.operation.server.config;

import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Optional;

/**
 * @author yangyang
 * @date 2024/2/24-13:58
 **/

public class UserThreadLocalUtil {

	private static final ThreadLocal<SimpleUserBO> USER_THREAD_LOCAL = new TransmittableThreadLocal<>();

	public static Optional<SimpleUserBO> getAndRemoveUser() {
		SimpleUserBO result = USER_THREAD_LOCAL.get();
		USER_THREAD_LOCAL.remove();
		return Optional.ofNullable(result);
	}

	public static void setUser(SimpleUserBO user) {
		USER_THREAD_LOCAL.set(user);
	}

	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	public static class SimpleUserBO {

		private String userNo;

		private String userName;
	}

}
